package org.ysu.shoeShopping.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.ysu.shoeShopping.dao.OrdersDao;
import org.ysu.shoeShopping.dao.OrderShoesDao;
import org.ysu.shoeShopping.dto.OrderRequest;
import org.ysu.shoeShopping.entity.Orders;
import org.ysu.shoeShopping.entity.OrderShoes;
import org.ysu.shoeShopping.service.OrdersService;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
import java.math.BigDecimal;

import javax.annotation.Resource;

/**
 * 订单表(Orders)表服务实现类
 *
 * @author makejava
 * @since 2025-09-13 16:59:21
 */
@Service("ordersService")
public class OrdersServiceImpl implements OrdersService
{
    @Resource
    private OrdersDao ordersDao;

    @Resource
    private OrderShoesDao orderShoesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param oid 主键
     * @return 实例对象
     */
    @Override
    public Orders queryById(Integer oid) {
        return this.ordersDao.queryById(oid);
    }

    /**
     * 分页查询
     *
     * @param orders 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Orders> queryByPage(Orders orders, PageRequest pageRequest) {
        long total = this.ordersDao.count(orders);
        return new PageImpl<>(this.ordersDao.queryAllByLimit(orders, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @Override
    public Orders insert(Orders orders) {
        this.ordersDao.insert(orders);
        return orders;
    }

    /**
     * 修改数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @Override
    public Orders update(Orders orders) {
        this.ordersDao.update(orders);
        return this.queryById(orders.getOid());
    }

    /**
     * 通过主键删除数据
     *
     * @param oid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer oid) {
        return this.ordersDao.deleteById(oid) > 0;
    }

    /**
     * 根据用户ID查询订单详情列表
     *
     * @param userId 用户ID
     * @return 订单详情列表
     */
    @Override
    public List<Orders> findByUserId(Integer userId) {
        return this.ordersDao.findByUserId(userId);
    }

    /**
     * 根据用户ID查询购物车订单（状态为0的订单）
     *
     * @param userId 用户ID
     * @return 购物车订单列表
     */
    @Override
    public List<Orders> findCartOrdersByUid(Integer userId) {
        return this.ordersDao.findCartOrdersByUid(userId);
    }


    @Override
    public List<Orders> findCartOrdersAll()
    {
        return this.ordersDao.findCartOrdersAll();
    }

    /**
     * 创建订单
     *
     * @param orderRequest 订单请求信息
     * @return 创建的订单
     */
    @Override
    public Orders createOrder(OrderRequest orderRequest) {
        try {
            // 创建订单
            Orders order = new Orders();
            order.setUserId(orderRequest.getUid());
            order.setReceiveId(orderRequest.getAddressId());
            order.setOrdertime(new Date());
            order.setOstate(1); // 设置订单状态为已创建

            // 生成订单编号
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String orderNumber = "ORD" + sdf.format(new Date()) + orderRequest.getUid();
            order.setOnum(orderNumber);

            // 使用前端传入的订单金额
            if (orderRequest.getPrincipal() != null) {
                order.setOtotal(orderRequest.getPrincipal().toString());
            } else {
                order.setOtotal("0");
            }

            // 设置订单积分
            if (orderRequest.getPoints() != null) {
                order.setOintegral(orderRequest.getPoints().toString());
            } else {
                order.setOintegral("0");
            }

            // 插入订单记录
            ordersDao.insert(order);

            // 创建订单商品记录
            for (OrderRequest.OrderItem item : orderRequest.getOrderItems()) {
                OrderShoes orderShoes = new OrderShoes();
                orderShoes.setOrderId(order.getOid());
                orderShoes.setShoeId(item.getProductId());
                orderShoes.setOsnum(item.getQuantity());
                orderShoes.setOssize(item.getSize() != null ? item.getSize() : "");
                orderShoes.setOsstate(1); // 设置状态

                orderShoesDao.insert(orderShoes);
            }

            return order;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
